package com.dynabook.ems.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dynabook.ems.annotation.LoginRequired;
import com.dynabook.ems.common.BaseResponse;
import com.dynabook.ems.common.ErrorCode;
import com.dynabook.ems.common.ResultUtils;
import com.dynabook.ems.exception.BusinessException;
import com.dynabook.ems.model.dto.*;
import com.dynabook.ems.model.vo.BomListVO;
import com.dynabook.ems.model.vo.OptionVO;
import com.dynabook.ems.model.vo.SelectCategoryVO;
import com.dynabook.ems.model.vo.UserLoginVO;
import com.dynabook.ems.service.EmsBomDetailService;
import com.dynabook.ems.service.EmsBomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * BOM 控制器
 */
@Api(tags = {"BOM 相关接口"})
@RestController
@RequestMapping("/bom")
@Slf4j
public class BomController {

    @Resource
    private EmsBomService emsBomService;

    @Resource
    private EmsBomDetailService emsBomDetailService;


    /**
     * 获取当前存在的成品型号
     */
    @ApiOperation("获取当前存在的成品型号")
    @PostMapping("/getExistProductName")
    public BaseResponse<List<OptionVO>> getExistProductName(@RequestBody @Validated GetExistProductNameRequest request) {
            if(request == null){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"成品型号前缀不能为空");
            }
            List<OptionVO> finishedProductModels = emsBomService.getExistProductName(request.getProductNamePrefix());
            return ResultUtils.success(finishedProductModels);

    }
    /**
     * 校验成品型号是否存在
     */
    @ApiOperation("校验成品型号是否存在")
    @PostMapping("/checkExistProductName")
    public BaseResponse<Boolean> checkExistProductName(@RequestBody @Validated CheckExistProductNameRequest request) {
        if(request == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"成品型号不能为空");
        }
        boolean exist = emsBomService.checkExistProductName(request.getFinishedProductModel());
        return ResultUtils.success(exist);
    }

    /**
     * 新增BOM
     *
     * @param bomAddRequest BOM添加请求
     * @param request HTTP请求
     * @return 操作结果
     */
    @ApiOperation("新增BOM")
    @PostMapping("/add")
    @LoginRequired
    public BaseResponse<HashMap<String, Boolean>> addBom(@Validated @RequestBody BomAddRequest bomAddRequest, HttpServletRequest request) {
        // 输出session中的用户信息，帮助诊断问题
        UserLoginVO userLoginVO = (UserLoginVO) request.getSession().getAttribute("user_login");
        log.info("BomController.addBom session中的用户信息: {}", userLoginVO);
        return emsBomService.addBom(bomAddRequest, request);
    }
    
    /**
     * 生成BOMID的方法
     */
    @ApiOperation("生成BOMID的方法")
    @PostMapping("/generateBomId")
    public BaseResponse<HashMap<String, String>> generateBomId() {
        HashMap<String, String> map = new HashMap<>();
        map.put("BOMID", emsBomService.generateBomId());
        return ResultUtils.success(map);
    }
    
    /**
     * 分页查询BOM列表
     *
     * @param bomListRequest 查询条件
     * @return BOM列表
     */
    @ApiOperation("分页查询BOM列表")
    @PostMapping("/list")
    public BaseResponse<Page<BomListVO>> getBomList(@RequestBody BomListRequest bomListRequest) {
        log.info("查询BOM列表，参数: {}", bomListRequest);
        Page<BomListVO> bomListPage = emsBomService.getBomList(bomListRequest);
        return ResultUtils.success(bomListPage);
    }

    /**
     * 根据BOMID查询BOM详细信息
     *
     * @param bomDetailRequest 查询条件
     * @return BOM详细信息
     */
    @ApiOperation("根据BOMID查询BOM详细信息")
    @PostMapping("/getBomDetailById")
    @LoginRequired
    public BaseResponse getBomDetailById(
                  @Validated  @RequestBody BomDetailRequest bomDetailRequest) {
        String bomId = bomDetailRequest.getBomId();
        return emsBomService.getBomDetailById(bomId);
    }

    /**
     * 批量删除BOM
     *
     * @param bomDeleteRequest 删除请求
     * @return 删除结果
     */
    @ApiOperation("批量删除BOM")
    @PostMapping("/delete")
    @LoginRequired
    public BaseResponse<Boolean> deleteBom(
            @Validated @RequestBody BomDeleteRequest bomDeleteRequest) {
        log.info("批量删除BOM，参数: {}", bomDeleteRequest);
        return emsBomService.deleteBomByIds(bomDeleteRequest);
    }

    /**
     * 根据BOM详情ID删除BOM详情
     * @param request
     * @return
     */

    @ApiOperation("根据BOM详情ID删除BOM详情")
    @PostMapping("/deleteDetail")
    @LoginRequired
    public BaseResponse<Boolean> deleteBomDetail(@RequestBody @Validated BomDetailDeleteRequest request) {
        return emsBomDetailService.deleteBomDetailById(request.getDetailId());
    }

    /**
     * BOM版本更新
     * @param bomCopyRequest
     * @param request
     * @return
     */
    @ApiOperation("BOM版本更新")
    @PostMapping("/bomVersionUpdate")
    @LoginRequired
    public BaseResponse<String> bomVersionUpdate(@RequestBody @Validated BomCopyRequest bomCopyRequest,HttpServletRequest request) {
        return emsBomService.copyBom(bomCopyRequest,request);
    }

    /**
     * 编辑BOM详情
     *
     * @param bomEditRequest 编辑请求
     * @return 编辑结果
     */
    @ApiOperation("编辑BOM详情")
    @PostMapping("/edit")
    @LoginRequired
    public BaseResponse<Boolean> editBom(@RequestBody @Validated BomEditRequest bomEditRequest) {
        log.info("编辑BOM详情，参数: {}", bomEditRequest);
        return emsBomService.editBom(bomEditRequest);
    }
    
    /**
     * 根据BOM编号和版本号查询BOM详细信息
     *
     * @param request 查询条件
     * @return BOM详细信息
     */
    @ApiOperation("根据BOM编号和版本号查询BOM详细信息")
    @PostMapping("/getBomDetailByNumAndVersion")
    @LoginRequired
    public BaseResponse getBomDetailByNumAndVersion(
            @Validated @RequestBody BomDetailByNumVersionRequest request) {
        String bomNum = request.getBomNum();
        String version = request.getVersion();
        return emsBomService.getBomDetailByNumAndVersion(bomNum, version);
    }
    
    /**
     * 根据成品型号和版本号查询BOM详细信息
     *
     * @param request 查询条件
     * @return BOM详细信息
     */
    @ApiOperation("根据成品型号和版本号查询BOM详细信息")
    @PostMapping("/getBomDetailByFinishedProductModelAndVersion")
    @LoginRequired
    public BaseResponse getBomDetailByFinishedProductModelAndVersion(
            @Validated @RequestBody BomDetailByFinishedProductModelVersionRequest request) {
        String finishedProductModel = request.getFinishedProductModel();
        String version = request.getVersion();
        String workOrderNumber = request.getWorkOrderNumber();
        log.info("根据成品型号和版本号查询BOM详细信息，成品型号: {}, 版本号: {}", finishedProductModel, version);
        return emsBomService.getBomDetailByFinishedProductModelAndVersion(finishedProductModel, version,workOrderNumber);
    }
    
    /**
     * 根据BOM编号查询版本选项列表
     *
     * @param request 查询条件
     * @return 版本选项列表
     */
    @ApiOperation("根据BOM编号查询版本选项列表")
    @PostMapping("/getVersionOptions")
    public BaseResponse<List<CategoryOptions>> getVersionOptionsByBomNum(
            @Validated @RequestBody BomVersionRequest request) {
        log.info("查询BOM版本选项，参数: {}", request);
        return emsBomService.getVersionOptionsByBomNum(request.getBomNum());
    }
    
    /**
     * 获取成品型号选项列表（去重）
     *
     * @return 成品型号选项列表
     */
    @ApiOperation("获取成品型号选项列表")
    @PostMapping("/getFinishedProductModelOptions")
    public BaseResponse<List<CategoryOptions>> getFinishedProductModelOptions() {
        log.info("查询成品型号选项列表");
        return emsBomService.getFinishedProductModelOptions();
    }

    /**
     * 获取样品型号选项列表（去重）
     *
     * @return 样品型号选项列表
     */
    @ApiOperation("获取样品型号选项列表")
    @PostMapping("/getTypeOptions")
    public BaseResponse<List<CategoryOptions>> getTypeOptions() {
        log.info("查询样品型号选项列表");
        return emsBomService.getTypeOptions();
    }

    /**
     * 搜索样品型号选项
     *
     * @return 样品型号选项列表
     */
    @ApiOperation("搜索样品型号选项")
    @PostMapping("/searchTypeOptions")
    public BaseResponse<List<CategoryOptions>> searchTypeOptions() {
        log.info("查询样品型号选项列表");
        return emsBomService.getTypeOptions();
    }

    
    /**
     * 根据成品型号查询版本选项列表
     *
     * @param request 查询条件
     * @return 版本选项列表
     */
    @ApiOperation("根据成品型号查询版本选项列表")
    @PostMapping("/getVersionOptionsByFinishedProductModel")
    public BaseResponse<SelectCategoryVO> getVersionOptionsByFinishedProductModel(
            @Validated @RequestBody FinishedProductModelRequest request) {
        log.info("根据成品型号查询版本选项，参数: {}", request);
        return emsBomService.getVersionOptionsByFinishedProductModel(request.getFinishedProductModel());
    }

    /**
     * 导出BOM数据
     *
     * @param bomExportRequest 导出请求参数
     * @param response HTTP响应对象
     */
    @ApiOperation("导出BOM数据")
    @PostMapping("/export")
    @LoginRequired
    public void exportBomData(@RequestBody BomExportRequest bomExportRequest, HttpServletResponse response) {
        log.info("导出BOM数据，参数: {}", bomExportRequest);
        emsBomService.exportBomData(bomExportRequest, response);
    }


 
}